package com.holly.common_view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.holly.common.utils.L;
import com.holly.common_view.R;
import com.holly.common_view.ToViewUtils;
import com.holly.common_view.adapter.BaseAdapter;
import com.holly.common_view.adapter.BaseHolder;
import com.holly.common_view.bean.CheckedEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 多选框
 * <p>
 * 2016/3/26 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */
public class MultiCheckBoxDialog1 extends Dialog {
    private final String TAG = L.createTag(this);
    private OnMultiCheckBoxListener mOnMultiCheckBoxListener;
    private TextView mTvOk;
    private RecyclerView mRecyclerView;
    private Adapter mAdapter;
    private boolean mCheckable;

    public MultiCheckBoxDialog1(@NonNull Context context, int column) {
        super(context, R.style.DialogStyle);
        //点击屏幕外关闭
        setCanceledOnTouchOutside(true);

        Context themeContext = getContext();
        //设置为底部
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);

        View view = LayoutInflater.from(themeContext).inflate(R.layout.dialog_check_box, null);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setContentView(view, layoutParams);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        mTvOk = (TextView) view.findViewById(R.id.tv_ok);
        GridLayoutManager manager = new GridLayoutManager(view.getContext(), column, LinearLayoutManager.VERTICAL, false);
        mAdapter = new Adapter();
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mAdapter);
        mTvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (mOnMultiCheckBoxListener != null) {
                    mOnMultiCheckBoxListener.onCheckBoxChanged(mAdapter.getList());
                }
            }
        });
    }

    public MultiCheckBoxDialog1(Context context) {
        this(context,6);
    }

    public void setCheckable(boolean checkable) {
        mCheckable = checkable;
    }

    public void setOnMultiCheckBoxListener(OnMultiCheckBoxListener onMultiCheckBoxListener) {
        mOnMultiCheckBoxListener = onMultiCheckBoxListener;
    }

    public void setList(@Nullable List<? extends CheckedEntity> list) {
        List<CheckedEntity> cloneList = new ArrayList<>();
        if (list != null) {
            for (CheckedEntity bean : list) {
                cloneList.add((CheckedEntity) bean.clone());
            }
        }
        mAdapter.setList(cloneList);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void show() {
        //设置为底部
        Window window = getWindow();
        WindowManager wm = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = width * 8 / 10;
        lp.height = height * 3 / 10;
        window.setAttributes(lp);
        super.show();
    }

    class Adapter extends BaseAdapter<CheckedEntity> {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = ToViewUtils.toView(parent, R.layout.item_check_box);
            return new Holder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            super.onBindViewHolder(holder, position);
            Holder vh = (Holder) holder;
            final CheckedEntity bean = mList.get(position);
            vh.mCheckBox.setText(bean.getName());
            vh.mCheckBox.setOnCheckedChangeListener(null);
            vh.mCheckBox.setChecked(bean.isChecked());
            vh.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    bean.setChecked(isChecked);
                }
            });
        }

        class Holder extends BaseHolder<Spec> {
            private final CheckBox mCheckBox;

            public Holder(View itemView) {
                super(itemView);
                mCheckBox = (CheckBox) itemView.findViewById(R.id.check_box);
                mCheckBox.setChecked(mCheckable);
                mCheckBox.setFocusable(mCheckable);
            }
        }
    }

    public interface OnMultiCheckBoxListener {
        void onCheckBoxChanged(List<CheckedEntity> list);
    }

    public class Spec {
        String name;
        boolean isChecked;

        public Spec(String name, boolean isChecked) {
            this.name = name;
            this.isChecked = isChecked;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean isChecked() {
            return isChecked;
        }

        public void setChecked(boolean checked) {
            isChecked = checked;
        }
    }
}
